var tabFilter,menu=[],liIndex,curNav,delMenu;

layui.define(["layer","jquery","element"],function(exports){
    var layer = layui.layer,
        $ = layui.jquery,
        element = layui.element,
        layId,
        Tab = function(){
            this.tabConfig = {
                closed : true,  
                openTabNum : undefined, 
                tabFilter : "bodyTab", 
                url : undefined 
            }
        };
    
    Tab.prototype.render = function() {
        var url = this.tabConfig.url;
        $.get(url,function(data){
            if($(".navBar").html() == ''){
                var _this = this;
                $(".navBar").html(navBar(data)).height($(window).height()-245);
                element.init();
                $(window).resize(function(){
                    $(".navBar").height($(window).height()-245);
                })
            }
        })
    };
    
    Tab.prototype.getLayId = function(title){
        $(".layui-tab-title.top_tab li").each(function(){
            if($(this).find("cite").text() == title){
                layId = $(this).attr("lay-id");
            }
        });
        return layId;
    };
    
    Tab.prototype.hasTab = function(title){
        var tabIndex = -1;
        $(".layui-tab-title.top_tab li").each(function(){
            if($(this).find("cite").text() == title){
                tabIndex = 1;
            }
        });
        return tabIndex;
    };
    
    var tabIdIndex = 0;
    Tab.prototype.tabAdd = function(_this){
        if(window.sessionStorage.getItem("menu")){
            // 菜单处理逻辑
        }
        
        var that = this;
        var closed = that.tabConfig.closed,
            openTabNum = that.tabConfig.openTabNum;
            tabFilter = that.tabConfig.tabFilter;
            
        if(_this.attr("target") == "_blank"){
            window.location.href = _this.attr("data-url");
        }else if(_this.attr("data-url") != undefined){
            var title = '';
            if(_this.find("i.iconfont,i.layui-icon").attr("data-icon") != undefined){
                if(_this.find("i.iconfont").attr("data-icon") != undefined){
                    title += '<i class="iconfont '+_this.find("i.iconfont").attr("data-icon")+'"></i>';
                }else{
                    title += '<i class="layui-icon">'+_this.find("i.layui-icon").attr("data-icon")+'</i>';
                }
            }
            
            if(that.hasTab(_this.find("cite").text()) == -1 && _this.siblings("dl.layui-nav-child").length == 0){
                return;
            }
            
            tabIdIndex++;
            title += '<cite>'+_this.find("cite").text()+'</cite>';
            title += '<i class="layui-icon layui-unselect layui-tab-close" data-id="'+tabIdIndex+'">&#x1006;</i>';
            
            element.tabAdd(tabFilter, {
                title : title,
                content :"<iframe src='"+_this.attr("data-url")+"' data-id='"+tabIdIndex+"'></iframe>",
                id : new Date().getTime()
            });
            
            var curmenu = {
                "icon" : _this.find("i.iconfont").attr("data-icon")!=undefined ? _this.find("i.iconfont").attr("data-icon") : _this.find("i.layui-icon").attr("data-icon"),
                "title" : _this.find("cite").text(),
                "href" : _this.attr("data-url"),
                "layId" : new Date().getTime()
            };
            menu.push(curmenu);
            window.sessionStorage.setItem("menu",JSON.stringify(menu)); 
            element.tabChange(tabFilter, that.getLayId(_this.find("cite").text()));
            that.tabMove();
        }else{
            var curmenu = {
                "icon" : _this.find("i.iconfont").attr("data-icon")!=undefined ? _this.find("i.iconfont").attr("data-icon") : _this.find("i.layui-icon").attr("data-icon"),
                "title" : _this.find("cite").text(),
                "href" : _this.attr("data-url")
            };
            window.sessionStorage.setItem("curmenu",JSON.stringify(curmenu));
            element.tabChange(tabFilter, that.getLayId(_this.find("cite").text()));
            that.tabMove();
        }
    };
    
    Tab.prototype.tabMove = function(){
        $(window).on("resize",function(){
            var topTabsBox = $("#top_tabs_box"),
                topTabsBoxWidth = $("#top_tabs_box").width(),
                topTabs = $("#top_tabs"),
                topTabsWidth = $("#top_tabs").width(),
                tabLi = topTabs.find("li.layui-this"),
                top_tabs = document.getElementById("top_tabs");
                
            if(topTabsWidth > topTabsBoxWidth){
                if(tabLi.position().left > topTabsBoxWidth || tabLi.position().left+topTabsBoxWidth > topTabsWidth){
                    topTabs.css("left",topTabsBoxWidth-topTabsWidth);
                }else{
                    topTabs.css("left",-tabLi.position().left);
                }
                
                var flag = false;
                var cur = {
                    x:0,
                    y:0
                };
                var nx,dx,x ;
                
                function down(){
                    flag = true;
                    var touch ;
                    if(event.touches){
                        touch = event.touches[0];
                    }else {
                        touch = event;
                    }
                    cur.x = touch.clientX;
                    dx = top_tabs.offsetLeft;
                }
                
                function move(){
                    var self=this;
                    window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
                    if(flag){
                        var touch ;
                        if(event.touches){
                            touch = event.touches[0];
                        }else {
                            touch = event;
                        }
                        nx = touch.clientX - cur.x;
                        x = dx+nx;
                        if(x > 0){
                            x = 0;
                        }else{
                            if(x < topTabsBoxWidth-topTabsWidth){
                                x = topTabsBoxWidth-topTabsWidth;
                            }else{
                                x = dx+nx;
                            }
                        }
                        top_tabs.style.left = x +"px";
                        document.addEventListener("touchmove",function(){
                            event.preventDefault();
                        },false);
                    }
                }
                
                function end(){
                    flag = false;
                }
                
                topTabs.on("mousemove",move);
                $(document).on("mouseup",end);
                topTabs.on("touchmove",move);
                topTabs.on("touchend",end);
            }else{
                topTabs.off("mousemove",move);
                topTabs.off("mouseup",end);
                topTabs.off("touchstart",down);
                topTabs.off("touchmove",move);
                topTabs.off("touchend",end);
                topTabs.removeAttr("style");
                return false;
            }
        }).resize();
    };
    
    var bodyTab = new Tab();
    exports("bodyTab",function(option){
        return bodyTab.set(option);
    });
});